<?php

namespace Lib;

class Http {

    protected $_url           = null;
    protected $_post          = false;
    protected $_postFields    = null;
    protected $_timeout       = 1;
    protected $_cookie        = null;
    protected $_cookieJar     = null;
    protected $_cookieFile    = null;
    protected $_header        = [];
    protected $_ua            = null;
    protected $_ssl           = false;
    protected $_refer         = false;
    protected $_type          = null;
    protected $_return_header = false;


    public function setUrl($url) {
        $this->_url = (string)$url;

        return $this;
    }

    public function setPostFields($post) {
        $this->_post = true;

        if (is_array($post)) {
            $this->_postFields = http_build_query($post);
        } elseif (is_string($post)) {
            $this->_postFields = $post;
        }

        return $this;
    }

    public function setTimeout($time) {
        $this->_timeout = (int)$time;

        return $this;
    }

    public function setCookie($cookie) {
        $this->_cookie = (string)$cookie;

        return $this;
    }

    public function setCookieJar($cookie) {
        $this->_cookieJar = (string)$cookie;

        return $this;
    }

    public function setCookieFile($cookie) {
        $this->_cookieFile = (string)$cookie;

        return $this;
    }


    public function setHeader(array $headers) {
        $this->_header = $headers;

        return $this;
    }

    /**
     * 发送内容
     *
     * @param string $body
     * @return \Lib\Http
     */
    public function setBody($body) {
        $this->_header[] = $body;

        return $this;
    }

    public function setUa($ua) {
        $this->_ua = trim($ua);
    }

    public function setSsl($ssl) {
        $this->_ssl = (bool)$ssl;
    }

    public function setRefer($refer) {
        $this->_refer = $refer;
    }

    public function setType($type) {
        $this->_type = $type;
    }

    public function isReturnHeader($a) {
        $this->_return_header = $a;
    }

    public function send() {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->_url);


        if ($this->_ua) {
            curl_setopt($ch, CURLOPT_USERAGENT, $this->_ua);
        }

        if ($this->_ssl) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->_ssl);
        }

        if ($this->_refer) {
            curl_setopt($ch, CURLOPT_REFERER, $this->_refer);
        }

        if ($this->_type) {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->_type);
        }

        if ($this->_return_header) {
            // 只需返回HTTP header
            //curl_setopt($ch, CURLOPT_HEADER, 1);
            curl_setopt($ch, CURLOPT_HEADER, $this->_return_header);
        }




        //curl_setopt($ch, ,"PUT");
        // 不直接输出
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->_timeout);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 防止302 盗链

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        // curl_setopt($ch, CURLOPT_SSLVERSION, 2);//设置SSL协议版本号

        if (!empty($this->_header)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $this->_header);
        }

        if (!empty($this->_cookie)) {
            curl_setopt($ch, CURLOPT_COOKIE, $this->_cookie);
        }

        if (!empty($this->_cookieJar)) {
            //curl_setopt($ch,CURLOPT_COOKIESESSION,true);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $this->_cookieJar);
        }

        if (!empty($this->_cookieFile)) {
            //curl_setopt($ch,CURLOPT_COOKIESESSION,true);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $this->_cookieFile);
        }

        if ($this->_post === true) {
            curl_setopt($ch, CURLOPT_POST, 1);

            if (!empty($this->_postFields)) {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_postFields);
            }
        }

        // 页面内容我们并不需要
        curl_setopt($ch, CURLOPT_NOBODY, 0);

        $response = curl_exec($ch);
        //var_dump(curl_error($ch));die();
        curl_close($ch);
        $this->reset();
        return $response;
    }


    /**
     * 重置
     */
    protected function reset() {
        $this->_url        = null;
        $this->_post       = false;
        $this->_postFields = null;
        $this->_timeout    = 1;
        $this->_header     = [];
    }

}
